package com.uniflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uniflow.common.Result;
import com.uniflow.entity.DataSync;
import com.uniflow.service.DataSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 数据同步管理控制器
 */
@Api(tags = "数据同步管理")
@RestController
@RequestMapping("/api/v1/sync")
public class DataSyncController {
    
    @Autowired
    private DataSyncService dataSyncService;
    
    /**
     * 分页查询数据同步任务
     */
    @ApiOperation("分页查询数据同步任务")
    @GetMapping("/page")
    public Result<IPage<DataSync>> getPageList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("同步类型") @RequestParam(required = false) String syncType,
            @ApiParam("状态") @RequestParam(required = false) String status,
            @ApiParam("分类") @RequestParam(required = false) String category,
            @ApiParam("关键词") @RequestParam(required = false) String keyword,
            HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        IPage<DataSync> result = dataSyncService.getPageList(pageNum, pageSize, syncType, status, category, keyword, tenantId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询数据同步任务
     */
    @ApiOperation("根据ID查询数据同步任务")
    @GetMapping("/{id}")
    public Result<DataSync> getById(@ApiParam("数据同步任务ID") @PathVariable String id,
                                    HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        DataSync dataSync = dataSyncService.getById(id, tenantId);
        return Result.success(dataSync);
    }
    
    /**
     * 根据名称查询数据同步任务
     */
    @ApiOperation("根据名称查询数据同步任务")
    @GetMapping("/name/{name}")
    public Result<DataSync> getByName(@ApiParam("数据同步任务名称") @PathVariable String name,
                                      HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        DataSync dataSync = dataSyncService.getByName(name, tenantId);
        return Result.success(dataSync);
    }
    
    /**
     * 根据外部ID查询数据同步任务
     */
    @ApiOperation("根据外部ID查询数据同步任务")
    @GetMapping("/external/{externalId}")
    public Result<DataSync> getByExternalId(@ApiParam("外部ID") @PathVariable String externalId,
                                            HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        DataSync dataSync = dataSyncService.getByExternalId(externalId, tenantId);
        return Result.success(dataSync);
    }
    
    /**
     * 根据业务键查询数据同步任务
     */
    @ApiOperation("根据业务键查询数据同步任务")
    @GetMapping("/business-key/{businessKey}")
    public Result<DataSync> getByBusinessKey(@ApiParam("业务键") @PathVariable String businessKey,
                                             HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        DataSync dataSync = dataSyncService.getByBusinessKey(businessKey, tenantId);
        return Result.success(dataSync);
    }
    
    /**
     * 根据同步类型查询数据同步任务
     */
    @ApiOperation("根据同步类型查询数据同步任务")
    @GetMapping("/sync-type/{syncType}")
    public Result<List<DataSync>> getBySyncType(@ApiParam("同步类型") @PathVariable String syncType,
                                                HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getBySyncType(syncType, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据状态查询数据同步任务
     */
    @ApiOperation("根据状态查询数据同步任务")
    @GetMapping("/status/{status}")
    public Result<List<DataSync>> getByStatus(@ApiParam("状态") @PathVariable String status,
                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getByStatus(status, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据分类查询数据同步任务
     */
    @ApiOperation("根据分类查询数据同步任务")
    @GetMapping("/category/{category}")
    public Result<List<DataSync>> getByCategory(@ApiParam("分类") @PathVariable String category,
                                                HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getByCategory(category, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据创建人查询数据同步任务
     */
    @ApiOperation("根据创建人查询数据同步任务")
    @GetMapping("/created-by/{createdBy}")
    public Result<List<DataSync>> getByCreatedBy(@ApiParam("创建人ID") @PathVariable String createdBy,
                                                 HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getByCreatedBy(createdBy, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询启用的数据同步任务
     */
    @ApiOperation("查询启用的数据同步任务")
    @GetMapping("/enabled")
    public Result<List<DataSync>> getEnabledSyncs(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getEnabledSyncs(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询可执行的数据同步任务
     */
    @ApiOperation("查询可执行的数据同步任务")
    @GetMapping("/executable")
    public Result<List<DataSync>> getExecutableSyncs(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getExecutableSyncs(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询需要执行的定时任务
     */
    @ApiOperation("查询需要执行的定时任务")
    @GetMapping("/scheduled")
    public Result<List<DataSync>> getScheduledSyncs(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getScheduledSyncs(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询正在运行的数据同步任务
     */
    @ApiOperation("查询正在运行的数据同步任务")
    @GetMapping("/running")
    public Result<List<DataSync>> getRunningSyncs(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getRunningSyncs(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询依赖的数据同步任务
     */
    @ApiOperation("查询依赖的数据同步任务")
    @GetMapping("/{syncId}/dependent")
    public Result<List<DataSync>> getDependentSyncs(@ApiParam("数据同步任务ID") @PathVariable String syncId,
                                                     HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getDependentSyncs(syncId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 创建数据同步任务
     */
    @ApiOperation("创建数据同步任务")
    @PostMapping
    public Result<DataSync> createDataSync(@RequestBody DataSync dataSync,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        dataSync.setTenantId(tenantId);
        DataSync result = dataSyncService.createDataSync(dataSync, currentUserId);
        return Result.success(result);
    }
    
    /**
     * 更新数据同步任务
     */
    @ApiOperation("更新数据同步任务")
    @PutMapping("/{id}")
    public Result<DataSync> updateDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                           @RequestBody DataSync dataSync,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        dataSync.setId(id);
        dataSync.setTenantId(tenantId);
        DataSync result = dataSyncService.updateDataSync(dataSync, currentUserId);
        return Result.success(result);
    }
    
    /**
     * 删除数据同步任务
     */
    @ApiOperation("删除数据同步任务")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                          Authentication authentication,
                                          HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        boolean result = dataSyncService.deleteDataSync(id, currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 批量删除数据同步任务
     */
    @ApiOperation("批量删除数据同步任务")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteDataSyncs(@RequestBody BatchDeleteRequest request,
                                                 Authentication authentication,
                                                 HttpServletRequest httpRequest) {
        String currentUserId = authentication.getName();
        String tenantId = (String) httpRequest.getAttribute("tenantId");
        boolean result = dataSyncService.batchDeleteDataSyncs(request.getIds(), currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 启用数据同步任务
     */
    @ApiOperation("启用数据同步任务")
    @PostMapping("/{id}/enable")
    public Result<Boolean> enableDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                          Authentication authentication,
                                          HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        boolean result = dataSyncService.enableDataSync(id, currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 禁用数据同步任务
     */
    @ApiOperation("禁用数据同步任务")
    @PostMapping("/{id}/disable")
    public Result<Boolean> disableDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        boolean result = dataSyncService.disableDataSync(id, currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 执行数据同步任务
     */
    @ApiOperation("执行数据同步任务")
    @PostMapping("/{id}/execute")
    public Result<Boolean> executeDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        boolean result = dataSyncService.executeDataSync(id, currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 停止数据同步任务
     */
    @ApiOperation("停止数据同步任务")
    @PostMapping("/{id}/stop")
    public Result<Boolean> stopDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                        Authentication authentication,
                                        HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        boolean result = dataSyncService.stopDataSync(id, currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 重置数据同步任务
     */
    @ApiOperation("重置数据同步任务")
    @PostMapping("/{id}/reset")
    public Result<Boolean> resetDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                         Authentication authentication,
                                         HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        boolean result = dataSyncService.resetDataSync(id, currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 复制数据同步任务
     */
    @ApiOperation("复制数据同步任务")
    @PostMapping("/{id}/copy")
    public Result<DataSync> copyDataSync(@ApiParam("数据同步任务ID") @PathVariable String id,
                                         @RequestBody CopyDataSyncRequest request,
                                         Authentication authentication,
                                         HttpServletRequest httpRequest) {
        String currentUserId = authentication.getName();
        String tenantId = (String) httpRequest.getAttribute("tenantId");
        DataSync result = dataSyncService.copyDataSync(id, request.getNewName(), currentUserId, tenantId);
        return Result.success(result);
    }
    
    /**
     * 批量更新状态
     */
    @ApiOperation("批量更新状态")
    @PutMapping("/batch/status")
    public Result<Boolean> batchUpdateStatus(@RequestBody BatchUpdateStatusRequest request,
                                              Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = dataSyncService.batchUpdateStatus(request.getIds(), request.getStatus(), currentUserId);
        return Result.success(result);
    }
    
    /**
     * 批量更新优先级
     */
    @ApiOperation("批量更新优先级")
    @PutMapping("/batch/priority")
    public Result<Boolean> batchUpdatePriority(@RequestBody BatchUpdatePriorityRequest request,
                                                Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = dataSyncService.batchUpdatePriority(request.getIds(), request.getPriority(), currentUserId);
        return Result.success(result);
    }
    
    /**
     * 批量更新分类
     */
    @ApiOperation("批量更新分类")
    @PutMapping("/batch/category")
    public Result<Boolean> batchUpdateCategory(@RequestBody BatchUpdateCategoryRequest request,
                                                Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = dataSyncService.batchUpdateCategory(request.getIds(), request.getCategory(), currentUserId);
        return Result.success(result);
    }
    
    /**
     * 批量更新排序
     */
    @ApiOperation("批量更新排序")
    @PutMapping("/batch/sort")
    public Result<Boolean> batchUpdateSort(@RequestBody BatchUpdateSortRequest request,
                                            Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = dataSyncService.batchUpdateSort(request.getSortData(), currentUserId);
        return Result.success(result);
    }
    
    /**
     * 检查名称是否可用
     */
    @ApiOperation("检查名称是否可用")
    @GetMapping("/check-name")
    public Result<Boolean> checkNameAvailable(@ApiParam("名称") @RequestParam String name,
                                               @ApiParam("排除的ID") @RequestParam(required = false) String excludeId,
                                               HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        boolean available = dataSyncService.isNameAvailable(name, excludeId, tenantId);
        return Result.success(available);
    }
    
    /**
     * 检查业务键是否存在
     */
    @ApiOperation("检查业务键是否存在")
    @GetMapping("/check-business-key")
    public Result<Boolean> checkBusinessKeyExists(@ApiParam("业务键") @RequestParam String businessKey,
                                                   @ApiParam("排除的ID") @RequestParam(required = false) String excludeId,
                                                   HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        boolean exists = dataSyncService.isBusinessKeyExists(businessKey, excludeId, tenantId);
        return Result.success(exists);
    }
    
    /**
     * 获取数据同步任务统计信息
     */
    @ApiOperation("获取数据同步任务统计信息")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> statistics = dataSyncService.getStatistics(tenantId);
        return Result.success(statistics);
    }
    
    /**
     * 按状态统计数据同步任务数量
     */
    @ApiOperation("按状态统计数据同步任务数量")
    @GetMapping("/statistics/status")
    public Result<List<Map<String, Object>>> getStatsByStatus(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = dataSyncService.getStatsByStatus(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按同步类型统计数据同步任务数量
     */
    @ApiOperation("按同步类型统计数据同步任务数量")
    @GetMapping("/statistics/sync-type")
    public Result<List<Map<String, Object>>> getStatsBySyncType(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = dataSyncService.getStatsBySyncType(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按分类统计数据同步任务数量
     */
    @ApiOperation("按分类统计数据同步任务数量")
    @GetMapping("/statistics/category")
    public Result<List<Map<String, Object>>> getStatsByCategory(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = dataSyncService.getStatsByCategory(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按创建人统计数据同步任务数量
     */
    @ApiOperation("按创建人统计数据同步任务数量")
    @GetMapping("/statistics/created-by")
    public Result<List<Map<String, Object>>> getStatsByCreatedBy(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = dataSyncService.getStatsByCreatedBy(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按时间统计数据同步任务数量
     */
    @ApiOperation("按时间统计数据同步任务数量")
    @GetMapping("/statistics/time")
    public Result<List<Map<String, Object>>> getStatsByTime(
            @ApiParam("开始时间") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam("时间单位") @RequestParam String timeUnit,
            HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = dataSyncService.getStatsByTime(startTime, endTime, timeUnit, tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按月份统计数据同步任务数量
     */
    @ApiOperation("按月份统计数据同步任务数量")
    @GetMapping("/statistics/month")
    public Result<List<Map<String, Object>>> getStatsByMonth(@ApiParam("年份") @RequestParam Integer year,
                                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = dataSyncService.getStatsByMonth(year, tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取执行成功率统计
     */
    @ApiOperation("获取执行成功率统计")
    @GetMapping("/statistics/success-rate")
    public Result<Map<String, Object>> getSuccessRateStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = dataSyncService.getSuccessRateStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取平均执行时间统计
     */
    @ApiOperation("获取平均执行时间统计")
    @GetMapping("/statistics/avg-execution-time")
    public Result<Map<String, Object>> getAvgExecutionTimeStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = dataSyncService.getAvgExecutionTimeStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取失败任务统计
     */
    @ApiOperation("获取失败任务统计")
    @GetMapping("/statistics/failure")
    public Result<Map<String, Object>> getFailureStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = dataSyncService.getFailureStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取待执行任务数量
     */
    @ApiOperation("获取待执行任务数量")
    @GetMapping("/statistics/pending-count")
    public Result<Long> getPendingCount(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Long count = dataSyncService.getPendingCount(tenantId);
        return Result.success(count);
    }
    
    /**
     * 获取正在运行任务数量
     */
    @ApiOperation("获取正在运行任务数量")
    @GetMapping("/statistics/running-count")
    public Result<Long> getRunningCount(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Long count = dataSyncService.getRunningCount(tenantId);
        return Result.success(count);
    }
    
    /**
     * 获取今日新增任务数量
     */
    @ApiOperation("获取今日新增任务数量")
    @GetMapping("/statistics/today-added-count")
    public Result<Long> getTodayAddedCount(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Long count = dataSyncService.getTodayAddedCount(tenantId);
        return Result.success(count);
    }
    
    /**
     * 获取今日执行任务数量
     */
    @ApiOperation("获取今日执行任务数量")
    @GetMapping("/statistics/today-executed-count")
    public Result<Long> getTodayExecutedCount(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Long count = dataSyncService.getTodayExecutedCount(tenantId);
        return Result.success(count);
    }
    
    /**
     * 获取本周统计
     */
    @ApiOperation("获取本周统计")
    @GetMapping("/statistics/weekly")
    public Result<Map<String, Object>> getWeeklyStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = dataSyncService.getWeeklyStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取本月统计
     */
    @ApiOperation("获取本月统计")
    @GetMapping("/statistics/monthly")
    public Result<Map<String, Object>> getMonthlyStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = dataSyncService.getMonthlyStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 查询数据同步任务分类列表
     */
    @ApiOperation("查询数据同步任务分类列表")
    @GetMapping("/categories")
    public Result<List<String>> getCategoryList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> categories = dataSyncService.getCategoryList(tenantId);
        return Result.success(categories);
    }
    
    /**
     * 查询数据同步任务类型列表
     */
    @ApiOperation("查询数据同步任务类型列表")
    @GetMapping("/sync-types")
    public Result<List<String>> getSyncTypeList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> syncTypes = dataSyncService.getSyncTypeList(tenantId);
        return Result.success(syncTypes);
    }
    
    /**
     * 查询数据源类型列表
     */
    @ApiOperation("查询数据源类型列表")
    @GetMapping("/source-types")
    public Result<List<String>> getSourceTypeList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> sourceTypes = dataSyncService.getSourceTypeList(tenantId);
        return Result.success(sourceTypes);
    }
    
    /**
     * 查询目标类型列表
     */
    @ApiOperation("查询目标类型列表")
    @GetMapping("/target-types")
    public Result<List<String>> getTargetTypeList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> targetTypes = dataSyncService.getTargetTypeList(tenantId);
        return Result.success(targetTypes);
    }
    
    /**
     * 查询标签列表
     */
    @ApiOperation("查询标签列表")
    @GetMapping("/tags")
    public Result<List<String>> getTagList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> tags = dataSyncService.getTagList(tenantId);
        return Result.success(tags);
    }
    
    /**
     * 查询相关数据同步任务
     */
    @ApiOperation("查询相关数据同步任务")
    @GetMapping("/{syncId}/related")
    public Result<List<DataSync>> getRelatedSyncs(@ApiParam("数据同步任务ID") @PathVariable String syncId,
                                                  HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getRelatedSyncs(syncId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询依赖关系
     */
    @ApiOperation("查询依赖关系")
    @GetMapping("/{syncId}/dependencies")
    public Result<List<Map<String, Object>>> getDependencies(@ApiParam("数据同步任务ID") @PathVariable String syncId,
                                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> dependencies = dataSyncService.getDependencies(syncId, tenantId);
        return Result.success(dependencies);
    }
    
    /**
     * 查询子任务
     */
    @ApiOperation("查询子任务")
    @GetMapping("/{parentId}/children")
    public Result<List<DataSync>> getChildSyncs(@ApiParam("父任务ID") @PathVariable String parentId,
                                                HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> list = dataSyncService.getChildSyncs(parentId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询父任务
     */
    @ApiOperation("查询父任务")
    @GetMapping("/{childId}/parent")
    public Result<DataSync> getParentSync(@ApiParam("子任务ID") @PathVariable String childId,
                                          HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        DataSync parent = dataSyncService.getParentSync(childId, tenantId);
        return Result.success(parent);
    }
    
    /**
     * 查询数据同步任务链
     */
    @ApiOperation("查询数据同步任务链")
    @GetMapping("/{syncId}/chain")
    public Result<List<DataSync>> getSyncChain(@ApiParam("数据同步任务ID") @PathVariable String syncId,
                                               HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<DataSync> chain = dataSyncService.getSyncChain(syncId, tenantId);
        return Result.success(chain);
    }
    
    // 请求数据结构
    
    public static class BatchDeleteRequest {
        private List<String> ids;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
    }
    
    public static class CopyDataSyncRequest {
        private String newName;
        
        public String getNewName() { return newName; }
        public void setNewName(String newName) { this.newName = newName; }
    }
    
    public static class BatchUpdateStatusRequest {
        private List<String> ids;
        private String status;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
    }
    
    public static class BatchUpdatePriorityRequest {
        private List<String> ids;
        private Integer priority;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public Integer getPriority() { return priority; }
        public void setPriority(Integer priority) { this.priority = priority; }
    }
    
    public static class BatchUpdateCategoryRequest {
        private List<String> ids;
        private String category;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public String getCategory() { return category; }
        public void setCategory(String category) { this.category = category; }
    }
    
    public static class BatchUpdateSortRequest {
        private List<Map<String, Object>> sortData;
        
        public List<Map<String, Object>> getSortData() { return sortData; }
        public void setSortData(List<Map<String, Object>> sortData) { this.sortData = sortData; }
    }
}